<template>
  <dialogPublic
    title="签名"
    :show.sync="showDialog"
    @doSubmit="toSign"
    width="500px"
    height="200px"
    @doClose="() => (showDialog = false)"
  >
    <editForm
      :formConfig="loginFormConfig"
      v-if="showDialog"
      ref="loginForm"
      :formData="loginFormData"
    />
  </dialogPublic>
</template>
<script>
import { verifyStaffInfo } from "@/api/twoTicketManagement/workTicket.js";
export default {
  props: {
    showTemporary: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    position: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  data() {
    return {
      temporaryPassword: this.$store.getters["dirtData"](
        "ticket_work_temporary_password"
      )[0].value,
      showDialog: false,
      currentItem: {},
      loginFormData: {},
      loginFormConfig: {
        // labelWidth: "140px",
        itemArr: [
          {
            label: "用户名",
            prop: "accountHide",
            type: "text",
            span: 20,
          },
          {
            label: "密码",
            prop: "passwordHide",
            type: "password",
            span: 20,
          },
          {
            label: "用户名",
            prop: "account",
            type: "text",
            span: 22,
            required: true,
            autofocus: true,
            autocomplete: "off",
          },
          {
            label: "密码",
            prop: "password",
            type: "password",
            required: true,
            span: 22,
            autocomplete: "off",
          },
        ],
      },
    };
  },
  methods: {
    // 单人签名-打开签名弹框
    openSign(obj) {
      this.currentItem = obj;
      this.showDialog = true;
    },
    // 多人签名-打开签名弹框
    openSigns(item, account) {
      this.currentItem = Object.assign(item, { isAccount: account });
      this.showDialog = true;
    },
    // 签名确定
    async toSign() {
      let form = await this.$refs.loginForm.getFormData();
      form.password = await this.$utils.encryptPassword(form.password);
      form.type = "ticket";
      verifyStaffInfo(form)
        .then(async (res) => {
          if (res.data.msgcode && res.data.msgcode == "noexist") {
            if (this.showTemporary) {
              // 临时人员签名校验
              let data = await this.$refs.loginForm.getCurrentData();
              if (data.password == this.temporaryPassword) {
                // 临时人员密码正确
                let obj = {
                  name: data.account,
                  account: data.account,
                };
                if (this.currentItem.isAccount) {
                  // 多人签名
                  if (this.currentItem.isAccount == data.account) {
                    this.doSigns(obj); // 多人签名-校验通过
                  } else {
                    this.$message.warning(
                      "请使用" + this.currentItem.name + "的账号签名"
                    );
                  }
                } else {
                  // 单人签名
                  this.doSign(obj); // 单人签名-校验通过
                }
              } else {
                this.$message.error("密码有误");
              }
            } else {
              this.$message.error("不存在该账号！");
            }
          } else {
            // 内单位、外单位签名校验
            let obj = {
              name: res.data.name,
              account: res.data.account,
            };
            if (this.currentItem.isAccount) {
              // 多人签名
              if (this.currentItem.isAccount == res.data.account) {
                let list = res.data.postUserVoList;
                let flag = list.findIndex((item) => {
                  return item.postName == "电力线路工作票签发人";
                });
                if (this.currentItem.pass) {
                  this.doSigns(obj); // 多人签名-校验通过
                } else if (flag == -1) {
                  // 该用户岗位没有签名权限
                  this.$message.warning(
                    "仅以下用户能签名：电力线路工作票签发人"
                  );
                } else {
                  this.doSigns(obj); // 多人签名-校验通过
                }
              } else {
                this.$message.warning(
                  "请使用" + this.currentItem.name + "的账号签名"
                );
              }
            } else {
              // 单人签名
              if (this.showTemporary) {
                // 内单位相应岗位、外单位人员、临时人员都签名校验
                if (this.currentItem.position == "任何人") {
                  this.doSign(obj); // 单人签名-校验通过
                } else {
                  let list = res.data.orgVoList;
                  let flag = list.findIndex((item) => {
                    return item.outOrg == "1";
                  });
                  if (flag == -1) {
                    // 内单位人员校验岗位
                    let list = res.data.postUserVoList;
                    let flag = list.findIndex((item) => {
                      return item.postName == this.currentItem.position;
                    });
                    if (flag == -1) {
                      // 该用户岗位没有签名权限
                      this.$message.warning(
                        "仅以下用户能签名：" +
                          this.currentItem.position +
                          "、外单位、临时人员"
                      );
                    } else {
                      this.doSign(obj); // 单人签名-校验通过
                    }
                  } else {
                    // 外单位人员
                    this.doSign(obj); // 单人签名-校验通过
                  }
                }
              } else {
                // 只有内单位相应岗位人员可以签字
                let list = res.data.postUserVoList;
                let flag = list.findIndex((item) => {
                  return item.postName == this.currentItem.position;
                });
                if (flag == -1) {
                  // 该用户岗位没有签名权限
                  this.$message.warning(
                    "仅以下用户能签名：" + this.currentItem.position
                  );
                } else {
                  this.doSign(obj); // 单人签名-校验通过
                }
              }
            }
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    // 单人签名-校验通过
    doSign(obj) {
      this.$emit("doSign", obj, this.currentItem);
      this.showDialog = false;
    },
    // 多人签名-校验通过
    doSigns(obj) {
      this.$emit("doSignName", obj, this.currentItem);
      this.showDialog = false;
    },
  },
};
</script>
<style lang="less" scoped>
/deep/ .el-col-20 {
  opacity: 0;
  position: absolute;
  width: 0;
  height: 0;
}
</style>